package com.ymatou.query.seller.trading.api.parameter;

import java.util.ArrayList;
import java.util.List;

import com.ymttest.common.enums.LogisticsTypeEnum;
import com.ymttest.common.enums.OrderTypeEnum;
import com.ymttest.utils.DataUtil;

public class RequestCovertor {
	public static SellerMainOrderListQueryModel SellerMainOrderListRequestArguments(
			GetSellerMainOrderListBean request) {
		SellerMainOrderListQueryModel model = new SellerMainOrderListQueryModel();
		model.setSellerId(request.getSellerId());
		model.setSalesRefundOrderOnly(DataUtil.Booleann
				.GetValueOrDefault(request.getSalesRefundOrderOnly()));
		model.setProductName(request.getProductName());
		model.setBuyerNickName(request.getBuyerNickName());
		//		model.setPaidInFull(request.getIsPaidInFull());
		model.setMainOrderId(request.getMainOrderId());
		model.setOrderId(request.getOrderId());
		model.setRemarkLevel(request.getRemarkLevel());
		model.setProductId(request.getProductId());
		model.setReceiverName(request.getReceiverName());
		model.setReceiverPhone(request.getReceiverPhone());

		model.setDeliveryCode(request.getDeliveryCode());

		model.setPagination(new Pagination(request.getCurrentPageNum(), request
				.getPageSize()));
		ConstructOrderAddTimeProperty(model, request.getAddBeginTime(),
				request.getAddEndTime(), request.getPaidBeginTime(),
				request.getPaidEndTime());
		ConstructOrderPaidTimeProperty(model, request.getPaidBeginTime(),
				request.getPaidEndTime());
		//		ConstructOrderTypeProperty(model, request.getOrderTypeList(),
		//				request.getIsShangouOrder());
		ConstructCatalogStatusProperty(model, request.getLogisticsTypeList(),
				request.getPackageDeliveryDomesticStatusFilter());

		model.setOrderStatus(request.getOrderStatusList());

		return model;
	}

	private static void ConstructOrderAddTimeProperty(
			SellerMainOrderListQueryModel model, String addBeginTime,
			String addEndTime, String paidBeginTime, String paidEndTime) {
		if (addBeginTime != null || addEndTime != null
				|| (paidBeginTime == null && paidEndTime == null)) {
			model.setAddBeginTime(addBeginTime == null
					? "1970-01-01"
					: addBeginTime);
			model.setAddEndTime(addEndTime == null ? "9999-01-01" : addEndTime);
		}
	}

	private static void ConstructOrderPaidTimeProperty(
			SellerMainOrderListQueryModel model, String paidBeginTime,
			String paidEndTime) {
		// 存在付款的开始或结束时间时，自动填充为空的属性(时间的最大/小值)
		if (paidBeginTime != null || paidEndTime != null) {
			model.setPaidBeginTime(paidBeginTime == null
					? "1970-01-01"
					: paidBeginTime);
			model.setPaidEndTime(paidEndTime == null
					? "9999-01-01"
					: paidEndTime);
		}
	}

	private static void ConstructOrderTypeProperty(
			SellerMainOrderListQueryModel model, List<Integer> orderType,
			Boolean isShangouOrder) {
		if (DataUtil.isNullOrEmpty(orderType)) // 空类型处理
		{
			model.setOrderType(1);
			model.setShangou(isShangouOrder);
			return;
		}

		if (orderType.size() == 1) // 单类型处理
		{
			//			ConstructOrderTypeSingleProperty(model, orderType.get(0));
			return;
		}

		//		ConstructOrderTypeMultipleProperty(model, orderType);
	}

	private static void ConstructOrderTypeSingleProperty(
			SellerMainOrderListQueryModel model, int orderType) {
		if (orderType == OrderTypeEnum.Spot.getValue()) {
			model.setOrderType(6);
		} else if (orderType == OrderTypeEnum.Spot.getValue()) {
			model.setOrderType(3);
			model.setShangou(true);
		} else if (orderType == OrderTypeEnum.Purchasing.getValue()) {
			model.setOrderType(7);
		} else {
			model.setOrderType(1);
		}
	}

	private static void ConstructOrderTypeMultipleProperty(
			SellerMainOrderListQueryModel model, List<Integer> orderTypeList) {
		if (orderTypeList.contains(OrderTypeEnum.Spot.getValue())
				&& orderTypeList.contains(OrderTypeEnum.Shangou.getValue()))// 如果同时包含现货与扫货订单并排除包含全部类型的情况
		{
			model.setOrderType(4);
		} else if (orderTypeList.contains(OrderTypeEnum.Spot.getValue())
				&& orderTypeList.contains(OrderTypeEnum.Purchasing.getValue()))// 如果同时包含现货与下单器订单并排除包含全部类型的情况
		{
			model.setOrderType(2);
			model.setShangou(false);
		} else if (orderTypeList.contains(OrderTypeEnum.Shangou.getValue())
				&& orderTypeList.contains(OrderTypeEnum.Purchasing.getValue()))// 如果同时包含扫货与下单器订单并排除包含全部类型的情况
		{
			model.setOrderType(5);
		}
	}

	private static void ConstructCatalogStatusProperty(
			SellerMainOrderListQueryModel model,
			List<Integer> logisticsTypeList,
			PackageDeliveryDomesticStatusFilter packageDeliveryDomesticStatusFilter) {
		if (packageDeliveryDomesticStatusFilter != null) {
			if (packageDeliveryDomesticStatusFilter.isDomesticDelivered()
					&& !packageDeliveryDomesticStatusFilter
							.isDomesticUndelivered()) {
				model.setDomesticDelivered(true);
			} else if (!packageDeliveryDomesticStatusFilter
					.isDomesticDelivered()
					&& packageDeliveryDomesticStatusFilter
							.isDomesticUndelivered()) {
				model.setDomesticDelivered(false);
			}
			List<Integer> catalogStatus = new ArrayList<Integer>();
			catalogStatus.add(LogisticsTypeEnum.PackageDelivery.getValue());
			model.setCatalogStatus(catalogStatus);
		} else {
			model.setCatalogStatus(logisticsTypeList);
		}
	}
}
